package com.sketch.service;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sketch.commom.PageResult;
import com.sketch.commom.Result;
import com.sketch.commom.utils.DateUtil;
import com.sketch.entity.RechargeRecord;
import com.sketch.entity.VipPrice;
import com.sketch.entity.dto.RechargeRecordDTO;
import com.sketch.mapper.RechargeRecordMapper;
import com.sketch.mapper.VipPriceMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RechargeRecordService {
    @Resource
    private RechargeRecordMapper rechargeRecordMapper;

    @Autowired
    private VipPriceMapper vipPriceMapper;

    public Result list(Long vipPriceId, String startDate, String endDate, Long uid, Integer page, Integer size) {
        LambdaQueryWrapper<RechargeRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!ObjectUtils.isEmpty(vipPriceId), RechargeRecord::getVipPriceId, vipPriceId)
                .eq(!ObjectUtils.isEmpty(uid), RechargeRecord::getUid, uid)
                .between(!ObjectUtils.isEmpty(startDate) && !ObjectUtils.isEmpty(endDate),
                        RechargeRecord::getCreateTime, DateUtil.getStart(startDate), DateUtil.getEnd(endDate));
        Page<RechargeRecord> sketchInfoPage = new Page<>(page, size);
        IPage<RechargeRecord> result = rechargeRecordMapper.selectPage(sketchInfoPage, queryWrapper);
        PageResult dtoPage = new PageResult<>();
        BeanUtils.copyProperties(result, dtoPage);
        List<RechargeRecordDTO> list = new ArrayList<>();
        if (ObjectUtils.isEmpty(result.getRecords())) {
            return Result.success(dtoPage);
        }
        result.getRecords().forEach(e -> {
            RechargeRecordDTO rechargeRecordDTO = new RechargeRecordDTO();
            BeanUtils.copyProperties(e, rechargeRecordDTO);
            list.add(rechargeRecordDTO);
        });
        dtoPage.setList(list);
        return Result.success().withData(dtoPage);
    }

    /**
     * 插入充值记录
     *
     * @return
     */
    public RechargeRecord add(Long vipId) {
        RechargeRecord entity = new RechargeRecord();
        entity.setUid(StpUtil.getLoginIdAsLong());
        VipPrice vipPrice = vipPriceMapper.selectById(vipId);
        Assert.notNull(vipPrice, "vip配置不存在");
        entity.setNickname(Convert.toStr(StpUtil.getExtra("nickName"),""));
        entity.setPrice(vipPrice.getPrice());
        entity.setVipPriceId(vipPrice.getId());
        entity.setCreateTime(new Date());
        entity.setCreateBy(0L);
        entity.setUpdateTime(new Date());
        entity.setUpdateBy(0L);
        entity.setOutTradeNo(IdUtil.getSnowflakeNextId());
        entity.setPayStatus(2);
        int insert = rechargeRecordMapper.insert(entity);
        Assert.isTrue(insert > 0, "操作失败，请重试");
        entity.setTitle(vipPrice.getName());
        return entity;
    }

    /**
     * 修改充值记录
     * 0支付成功 1支付失败
     * @return
     */
    public int update(String outTradeNo,Integer payStatus) {
        RechargeRecord rechargeRecord = rechargeRecordMapper.selectOne(Wrappers.<RechargeRecord>lambdaQuery().eq(RechargeRecord::getOutTradeNo, outTradeNo));
        Assert.notNull(rechargeRecord, "交易记录不存在");
        rechargeRecord.setUpdateTime(new Date());
        rechargeRecord.setPayStatus(payStatus);
        return rechargeRecordMapper.updateById(rechargeRecord);
    }
}
